package cn.fl.insurance.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsPlcStlDtlQueryCondition;
import cn.fl.insurance.dto.InsPlcStlDtlDTO;
import cn.fl.insurance.service.IInsPlcStlDtlApiService;
import cn.fl.insurance.service.IInsPlcStlDtlService;
import cn.fl.insurance.transform.InsPlcStlDtlVOTransform;
import cn.fl.insurance.vo.InsPlcStlDtlVO;

/**
 * @Description: 保险公司保单结算明细 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsPlcStlDtlServiceImpl implements IInsPlcStlDtlService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsPlcStlDtlServiceImpl.class);

    /**
     * TODO 自定义 API 暴露服务
     */
    @Autowired
    private IInsPlcStlDtlApiService insPlcStlDtlApiService;

    @Override
    public InsPlcStlDtlVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        InsPlcStlDtlDTO dto = this.insPlcStlDtlApiService.selectInsPlcStlDtlById(id);
        return InsPlcStlDtlVOTransform.toVO(dto);
    }

    @Override
    public Long add(InsPlcStlDtlVO entity) throws BaseException {
        if (null == entity) return null;
        return this.insPlcStlDtlApiService.addInsPlcStlDtl(InsPlcStlDtlVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<InsPlcStlDtlVO> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.insPlcStlDtlApiService.removeInsPlcStlDtlByIds(InsPlcStlDtlVOTransform.toDTOList(ids), CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(InsPlcStlDtlVO entity) throws BaseException {
        if (null == entity) return false;
        return this.insPlcStlDtlApiService.modifyInsPlcStlDtl(InsPlcStlDtlVOTransform.toDTO(entity));
    }

    /**
     * 修改
     *
     * @param list
     * @return
     */
    public boolean modify(List<InsPlcStlDtlVO> list) throws BaseException {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return this.insPlcStlDtlApiService.modifyInsPlcStlDtl(InsPlcStlDtlVOTransform.toDTOList(list));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page = this.insPlcStlDtlApiService.searchInsPlcStlDtlListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<InsPlcStlDtlVO> voList = InsPlcStlDtlVOTransform.toVOList((List<InsPlcStlDtlDTO>) page.getResultList());
            for (InsPlcStlDtlVO vo : voList) {
                if (StringUtils.isNotEmpty(vo.getActStlAmt())) {
                    vo.setActStlAmtDb(DecimalUtils.div(vo.getActStlAmt(), 100d));//实际结算金额
                } else {
                    vo.setActStlAmtDb(0d);//实际结算金额
                }
                if (StringUtils.isEmpty(vo.getStlAmt())) {
                    vo.setStlAmt(0L);
                }
                if (null == vo.getStlRat()) {
                    vo.setStlRat(0d);
                }
            }
            page.setResultList(voList);
        }
        return page;
    }

    @Override
    public List<InsPlcStlDtlVO> searchList(InsPlcStlDtlQueryCondition condition) throws BaseException {
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        List<InsPlcStlDtlVO> voList =InsPlcStlDtlVOTransform.toVOList(this.insPlcStlDtlApiService.searchInsPlcStlDtlList(condition));
        if(CollectionUtils.isNotEmpty(voList)){
            for (InsPlcStlDtlVO vo : voList) {
                if (StringUtils.isNotEmpty(vo.getActStlAmt())) {
                    vo.setActStlAmtDb(DecimalUtils.div(vo.getActStlAmt(), 100d));//实际结算金额
                } else {
                    vo.setActStlAmtDb(0d);//实际结算金额
                }
                if (StringUtils.isEmpty(vo.getStlAmt())) {
                    vo.setStlAmt(0L);
                }
                if (null == vo.getStlRat()) {
                    vo.setStlRat(0d);
                }
            }
        }
        return voList;
    }

}
